package com.qfedu.fmmall.controller;

import com.qfedu.fmmall.utils.Base64Utils;
import com.qfedu.fmmall.vo.ResultVO;
import io.jsonwebtoken.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController//相当于@Controller+@ResponseBody
@RequestMapping("/shopcart")
@Api(value = "提供购物车业务相关接口",tags = "购物车管理")//swagger
@CrossOrigin//跨域
public class ShopcartController {

    @GetMapping("/list")
    @ApiImplicitParam(dataType = "string",name = "token", value = "授权令牌",required = true)
    public ResultVO listCarts(String token){
        //1:获取token:从参数就能获取
        //2:校验token(模拟)
        if(token==null){
            return new ResultVO(10001,"请先登录",null);//模拟
        }else {

            try {
                JwtParser parser= Jwts.parser();//获得一个解析器
                parser.setSigningKey("QIANfeng6666");//解析时的Key必须和设置token时的密码一致
                //如果token正确（密码正确，有效期内）则正常执行，否则抛出异常
                Jws<Claims> claimsJws = parser.parseClaimsJws(token);
                Claims body = claimsJws.getBody();//获取token中的用户数据
                String subject=body.getSubject();//获取token中的用户数据
                String v1 = body.get("key1", String.class);//获取生成token设置的map值


                //token校验成功
                return new ResultVO(10000,"success",null);


            } catch (ExpiredJwtException e){
                return new ResultVO(10001,"登录过期，请重新登陆!",null);
            } catch (UnsupportedJwtException e){
                return new ResultVO(10001,"Token不合法",null);
            }catch (Exception e){
                return new ResultVO(10001,"请重新登陆",null);
            }

        }

    }
}
